﻿
IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_File_DeleteDirectory_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_File_DeleteDirectory_V2];
GO

CREATE PROCEDURE [dbo].[sproc_File_DeleteDirectory_V2]
    @ownerId          int,
    @directoryId      int
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

IF NOT EXISTS (SELECT 1 FROM dbo.UDS_File_Directory WHERE DirectoryId = @directoryId AND OwnerId = @ownerId )
BEGIN
    RAISERROR(N'指定目录不存在。', 16, 1);
    RETURN -1;
END

IF EXISTS ( SELECT 1 FROM dbo.UDS_File_Directory WHERE ParentDirectoryId = @directoryId )
BEGIN
    RAISERROR(N'指定目录不为空。', 16, 1);
    RETURN -2;
END

IF EXISTS ( SELECT 1 FROM dbo.UDS_File_File WHERE DirectoryId = @directoryId )
BEGIN
    RAISERROR(N'指定目录不为空。', 16, 1);
    RETURN -2;
END

DELETE
    FROM dbo.UDS_File_Directory
    WHERE DirectoryId = @DirectoryId AND OwnerId = @OwnerId;

END
GO

